package com.fenghuajueli.module_user.mine

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import com.fenghuajueli.module_user.fragment.UserMineFragment
import com.fenghuajueli.module_user.model.RequestInfoModel
import com.fenghuajueli.module_user.network.repository.TemplateRepository
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 * @Author: sz_ijj
 * @Date: 2024-01-06 14:57
 **/
class UserMineViewModel : ViewModel() {
    val templateResult = TemplateRepository.templateData.asSharedFlow()

    private val _isLoading = MutableSharedFlow<Boolean>(0)
    val isLoading = _isLoading.asSharedFlow()

    private val _userUiState = MutableStateFlow(UserUiState())
    val userUiState = _userUiState.asStateFlow()

    fun refreshInfo() = viewModelScope.launch {
        val user = UserInfoUtils.getInstance()
        _userUiState.emit(
            UserUiState(
                payStatus = SwitchKeyUtils.getPayStatus(),
                isVip = user.isVip,
                isLogin = user.isLogin,
                mDot = redDotState(),
                isLocalHead = updateViewsContent(),
                headIcon = getHeadIconUrl(),
                icp = AppConfigInfo.ICP
            )
        )
    }

    fun fakeLoading() = viewModelScope.launch {
        delay(1500)
        _isLoading.emit(true)
    }

    fun saveDot() = viewModelScope.launch {
        MmkvUtils.save(KEY_RED_DOT_CLICKED, true)
        _userUiState.update { it.copy(mDot = false) }
    }

    fun getUserName() =
        if (UserInfoUtils.getInstance().isLogin)
            UserInfoUtils.getInstance().userInfoEntity.userInfo.user_name
        else
            "登录/注册"

    private fun getHeadIconUrl() = MmkvUtils.get(LOCAL_AVATAR_KIND, "")

    /**
     * @see UserMineFragment.updateViewsContent
     */
    private fun updateViewsContent(): Boolean =
        getHeadIconUrl().isBlank() || !UserInfoUtils.getInstance().isLogin() && MmkvUtils.get(
            RequestInfoModel.LOGIN_OUT,
            false
        )

    /**
     * @see UserMineFragment.redDotState
     */
    private fun redDotState(): Boolean {
        val lastVersionCode = MmkvUtils.get(KEY_LAST_VERSION_CODE, -1)
        val clicked = MmkvUtils.get(KEY_RED_DOT_CLICKED, false)
        return if (AppConfigInfo.VERSION_CODE > lastVersionCode) {
            MmkvUtils.save(KEY_LAST_VERSION_CODE, AppConfigInfo.VERSION_CODE)
            MmkvUtils.save(KEY_RED_DOT_CLICKED, false)
            true
        } else if (!clicked) {
            true
        } else {
            false
        }
    }

    private val LOCAL_AVATAR_KIND = "local_avatar_kind"

    private val KEY_RED_DOT_CLICKED = "key_red_dot_clicked"

    private val KEY_LAST_VERSION_CODE = "key_last_app_version_code"
}

data class UserUiState(
    val payStatus: Boolean = true,
    val isVip: Boolean = false,
    val isLogin: Boolean = false,
    val mDot: Boolean = true,
    val isLocalHead: Boolean = true,
    val headIcon: String = "",
    val icp: String = "",
)